package com.fuershiye.woman.monitor.utils;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

public class MyUtilString {

	public static final String MYUTIL_TYPE = "格式";
	public static final String MYUTIL_VALUE = "数据";
	public static final String MYUTIL_MARK = "备注";
	public static final String INAME_APPEND = "-";

	/**
	 * 判断 isBlank,null,Null,NULL,0
	 *
	 * @param str
	 * @return
	 */
	public static String getSubIname(String str) {

		if(str.isEmpty()||str.length()<1){
			return "";
		}

		if(!str.startsWith(INAME_APPEND)){
			return str;
		}

		return str.replaceAll(INAME_APPEND,"");
	}

	/**
	 * 判断 isBlank,null,Null,NULL,0
	 *
	 * @param str
	 * @return
	 */
	public static boolean isBlankNullZero(String str) {

		if(StringUtils.isBlank(str)){
			return true;
		}
		if(str.equalsIgnoreCase("null")){
			return true;
		}
		if(str.equals("0")){
			return true;
		}
		return false;
	}

	
	/**
	 * 判断 isBlank,null,Null,NULL
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBlankNull(String str) {
		
		if(StringUtils.isBlank(str)){
			return true;
		}
		if(str.equalsIgnoreCase("null")){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断 是否为 null,返回 空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String nullToBlank(String str) {
		
		if(isBlankNull(str)){
			return "";
		}
		
		return str;
	}
	
	/**
	 * 判断 是否为 null,返回 空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static Object nullToZero(String str) {
		
		if(isBlankNull(str)){
			return null;
		}
		return Integer.parseInt(str);
	}
	
	public static String nullToEmpty(String str) {
		
		if(StringUtils.isBlank(str)){
			return "";
		}
		return str;
	}
	
	/**
	 * @param orgStr			源字符串
	 * @param placeHolder	占位符
	 * @return
	 */
	public static List<Long> str2LongByPlaceHolder(String orgStr, String placeHolder) {
		
		List<Long> result = Lists.newArrayList();
		if(StringUtils.isBlank(orgStr)){
			return null;
		}
		//只有当前元素
		if(!orgStr.contains(placeHolder)){
			result.add(Long.valueOf(orgStr));
			return result;
		}
		String [] temp = orgStr.split(placeHolder);
		if(temp==null || temp.length<1){
			return null;
		}
		for (String str : temp) {
			result.add(Long.parseLong(str));
		}
		return result;
	}


	/**
	 * @return
	 */
	public static List<Long> removeValue(List<Long> list, String replace) {


		if(list.size()<1){
			return list;
		}

		for (Long a : list ) {
			if(String.valueOf(a).equals(replace)){
				list.remove(a);
				break;
			}
		}

		return list;
	}


	public static String remark2Json(String old, String year, String now) {

		if(StringUtils.isEmpty(now)||StringUtils.isEmpty(year)){
			return old;
		}

		Map<String, Object> map = Maps.newHashMap();
		if(StringUtils.isEmpty(old)){
			map.put(year, now);
			return JSONArray.toJSONString(map);
		}

		try {
			map = JSONArray.parseObject(old);
			map.put(year, now);
			return JSONArray.toJSONString(map);
		}catch (Exception e){

		}
		return null;

	}


	public static String subStr(String str, String scanf) {

		if(!str.contains(scanf)){
			return null;
		}
		return str.substring(0, str.indexOf(scanf));

	}

	public static void main(String[] args) throws Exception {


		String str = "1231,143,-1,3243";

		List<Long> list = str2LongByPlaceHolder(str,",");
		System.out.println(list);

		System.out.println(removeValue(list, "-1"));

//		System.out.println(getSubIname(""));
//		System.out.println(getSubIname("A01"));
//		System.out.println(getSubIname("A01-"));
//		System.out.println(getSubIname("A-01-"));
//		System.out.println(getSubIname("-A01"));
//		System.out.println(getSubIname("--A01"));
//		System.out.println(getSubIname("----A01"));
//		System.out.println(getSubIname("--------A01"));

//		System.out.println(subStr("2010类型","类型"));
//
//		System.out.println(isBlankNullZero(""));
//		System.out.println(isBlankNullZero("null"));
//		System.out.println(isBlankNullZero("Null"));
//		System.out.println(isBlankNullZero("NUll"));
//		System.out.println(isBlankNullZero("NULL"));
//		System.out.println(isBlankNullZero("0"));
//		System.out.println(isBlankNullZero("NUL"));
	}

}